```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解 Java synchronized 机制</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', 'Noto Serif SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f8fafc;
            color: #1e293b;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .icon-box {
            width: 60px;
            height: 60px;
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .drop-cap::first-letter {
            font-size: 4.5rem;
            float: left;
            line-height: 0.65;
            margin: 0.1em 0.2em 0 0;
            font-weight: 700;
            color: #6366f1;
        }
        .code-block {
            background-color: #1e293b;
            color: #e2e8f0;
            border-radius: 0.5rem;
            font-family: 'Courier New', Courier, monospace;
        }
        .comparison-card:hover .comparison-icon {
            transform: scale(1.1);
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 sm:px-6 lg:px-8">
        <div class="max-w-4xl mx-auto text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-6">深入理解 Java <span class="bg-white text-indigo-600 px-3 py-1 rounded-lg">synchronized</span> 机制</h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto mb-8">探索 JVM 如何通过 Monitor 机制实现线程同步及其性能优化策略</p>
            <div class="flex justify-center space-x-4">
                <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full">偏向锁</span>
                <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full">轻量级锁</span>
                <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full">重量级锁</span>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-lg p-8">
                <p class="text-lg text-slate-600 drop-cap leading-relaxed">
                    synchronized 的底层实现依赖于 JVM 中的 Monitor 机制，结合了偏向锁、轻量级锁和重量级锁等优化策略，以提高并发性能。虽然 synchronized 使用方便、语义明确，但在高并发场景下可能面临性能瓶颈，特别是在锁争用激烈的情况下。
                </p>
            </div>
        </section>

        <!-- JVM Implementation -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="icon-box bg-indigo-100 text-indigo-600 mr-4">
                    <i class="fas fa-microchip text-2xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-slate-800">JVM 层面的实现</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-8">
                <div class="bg-white rounded-xl shadow-md p-6 card-hover transition-all duration-300">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-indigo-100 text-indigo-600 flex items-center justify-center mr-3">
                            <i class="fas fa-code"></i>
                        </div>
                        <h3 class="text-xl font-semibold">方法级别同步</h3>
                    </div>
                    <p class="text-slate-600 leading-relaxed">
                        如果在方法声明上使用 <code class="bg-indigo-50 text-indigo-600 px-1 rounded">synchronized</code>，JVM 会在 <code class="bg-indigo-50 text-indigo-600 px-1 rounded">Method</code> 的 <code class="bg-indigo-50 text-indigo-600 px-1 rounded">method_info</code> 结构中设置一个 <code class="bg-indigo-50 text-indigo-600 px-1 rounded">ACC_SYNCHRONIZED</code> 标志。当方法被调用时，JVM 会自动获取与该方法对应的 <code class="bg-indigo-50 text-indigo-600 px-1 rounded">Monitor</code> 对象锁。
                    </p>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-6 card-hover transition-all duration-300">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-indigo-100 text-indigo-600 flex items-center justify-center mr-3">
                            <i class="fas fa-brackets-curly"></i>
                        </div>
                        <h3 class="text-xl font-semibold">代码块级别同步</h3>
                    </div>
                    <p class="text-slate-600 leading-relaxed">
                        代码块级别的 <code class="bg-indigo-50 text-indigo-600 px-1 rounded">synchronized</code> 是通过字节码指令 <code class="bg-indigo-50 text-indigo-600 px-1 rounded">monitorenter</code> 和 <code class="bg-indigo-50 text-indigo-600 px-1 rounded">monitorexit</code> 实现的。每个对象都有一个与之关联的 <code class="bg-indigo-50 text-indigo-600 px-1 rounded">Monitor</code>，当线程进入同步代码块时，<code class="bg-indigo-50 text-indigo-600 px-1 rounded">monitorenter</code> 指令试图获取对象的 <code class="bg-indigo-50 text-indigo-600 px-1 rounded">Monitor</code>。
                    </p>
                </div>
            </div>
            
            <div class="bg-slate-800 rounded-xl p-6 mb-8">
                <div class="flex items-center text-white mb-4">
                    <i class="fas fa-terminal mr-2 text-indigo-300"></i>
                    <h3 class="font-mono font-semibold">字节码示例</h3>
                </div>
                <pre class="code-block p-4 overflow-x-auto">
<code class="text-green-300">// Java 源代码
public synchronized void syncMethod() {
    // 方法体
}

public void syncBlock() {
    synchronized(this) {
        // 同步块
    }
}

// 对应的字节码
public synchronized void syncMethod();
  descriptor: ()V
  flags: ACC_PUBLIC, ACC_SYNCHRONIZED
  Code:
    stack=0, locals=1, args_size=1
    0: return

public void syncBlock();
  descriptor: ()V
  flags: ACC_PUBLIC
  Code:
    stack=2, locals=3, args_size=1
    0: aload_0
    1: dup
    2: astore_1
    3: monitorenter
    4: aload_1
    5: monitorexit
    6: goto          14
    9: astore_2
    10: aload_1
    11: monitorexit
    12: aload_2
    13: athrow
    14: return</code>
                </pre>
            </div>
        </section>

        <!-- Lock Upgrade Mechanism -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="icon-box bg-purple-100 text-purple-600 mr-4">
                    <i class="fas fa-layer-group text-2xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-slate-800">锁的升级与优化机制</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md p-6 mb-8">
                <div class="mermaid">
                    flowchart LR
                        A[无锁状态] -->|首次获取锁| B[偏向锁]
                        B -->|有其他线程竞争| C[轻量级锁]
                        C -->|竞争加剧| D[重量级锁]
                        D -.->|不可降级| C
                </div>
            </div>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white rounded-xl shadow-md p-6 card-hover transition-all duration-300">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-purple-100 text-purple-600 flex items-center justify-center mr-3">
                            <i class="fas fa-thumbtack"></i>
                        </div>
                        <h3 class="text-xl font-semibold">偏向锁</h3>
                    </div>
                    <p class="text-slate-600 leading-relaxed mb-4">
                        偏向锁是 <code class="bg-purple-50 text-purple-600 px-1 rounded">synchronized</code> 的一种优化策略，用于在没有锁竞争的情况下减少加锁和解锁的开销。
                    </p>
                    <div class="bg-purple-50 rounded-lg p-3 text-sm">
                        <p class="text-purple-700 font-medium">工作原理:</p>
                        <p class="text-purple-600">当一个线程第一次获得对象锁时，JVM 会将偏向锁的标志设置为该线程，后续该线程再次进入同步代码块时，不需要执行 CAS 操作。</p>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-6 card-hover transition-all duration-300">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-purple-100 text-purple-600 flex items-center justify-center mr-3">
                            <i class="fas fa-weight-hanging"></i>
                        </div>
                        <h3 class="text-xl font-semibold">轻量级锁</h3>
                    </div>
                    <p class="text-slate-600 leading-relaxed mb-4">
                        轻量级锁是 <code class="bg-purple-50 text-purple-600 px-1 rounded">synchronized</code> 在遇到竞争时使用的一种优化策略。它通过使用 CAS 操作来减少线程竞争时的性能损耗。
                    </p>
                    <div class="bg-purple-50 rounded-lg p-3 text-sm">
                        <p class="text-purple-700 font-medium">升级条件:</p>
                        <p class="text-purple-600">当偏向锁失效时，JVM 会尝试将偏向锁升级为轻量级锁。轻量级锁在锁竞争不激烈的情况下非常高效。</p>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-6 card-hover transition-all duration-300">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-purple-100 text-purple-600 flex items-center justify-center mr-3">
                            <i class="fas fa-dumbbell"></i>
                        </div>
                        <h3 class="text-xl font-semibold">重量级锁</h3>
                    </div>
                    <p class="text-slate-600 leading-relaxed mb-4">
                        当锁竞争变得激烈时，轻量级锁会升级为重量级锁。这时，JVM 会通过操作系统的互斥量 (mutex) 进行线程调度。
                    </p>
                    <div class="bg-purple-50 rounded-lg p-3 text-sm">
                        <p class="text-purple-700 font-medium">性能影响:</p>
                        <p class="text-purple-600">重量级锁的获取和释放开销较高，会导致线程上下文切换，适合高并发竞争场景。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Memory Semantics -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="icon-box bg-blue-100 text-blue-600 mr-4">
                    <i class="fas fa-memory text-2xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-slate-800">内存语义</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md p-6 mb-8">
                <div class="mermaid">
                    sequenceDiagram
                        participant Thread as 线程
                        participant Monitor as Monitor
                        Thread->>Monitor: monitorenter (Acquire屏障)
                        Note right of Thread: 禁止读操作重排序到屏障后
                        Thread->>Monitor: 执行同步代码块
                        Thread->>Monitor: monitorexit (Release屏障)
                        Note right of Thread: 禁止写操作重排序到屏障前
                </div>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white rounded-xl shadow-md p-6 card-hover transition-all duration-300">
                    <h3 class="text-xl font-semibold mb-4">进入同步块 (<code class="bg-blue-50 text-blue-600 px-1 rounded">monitorenter</code>)</h3>
                    <p class="text-slate-600 leading-relaxed">
                        执行 <code class="bg-blue-50 text-blue-600 px-1 rounded">Acquire</code> 内存屏障，确保在进入同步块之前，所有对共享变量的读操作都不会被重排序到 <code class="bg-blue-50 text-blue-600 px-1 rounded">monitorenter</code> 之后。
                    </p>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-6 card-hover transition-all duration-300">
                    <h3 class="text-xl font-semibold mb-4">退出同步块 (<code class="bg-blue-50 text-blue-600 px-1 rounded">monitorexit</code>)</h3>
                    <p class="text-slate-600 leading-relaxed">
                        执行 <code class="bg-blue-50 text-blue-600 px-1 rounded">Release</code> 内存屏障，确保在退出同步块之前，所有对共享变量的写操作都不会被重排序到 <code class="bg-blue-50 text-blue-600 px-1 rounded">monitorexit</code> 之前。
                    </p>
                </div>
            </div>
        </section>

        <!-- Comparison with Other Mechanisms -->
        <section>
            <div class="flex items-center mb-8">
                <div class="icon-box bg-amber-100 text-amber-600 mr-4">
                    <i class="fas fa-balance-scale text-2xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-slate-800">synchronized 与其他并发机制的对比</h2>
            </div>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white rounded-xl shadow-md p-6 comparison-card transition-all duration-300 group">
                    <div class="flex items-center justify-center mb-4">
                        <div class="w-16 h-16 rounded-full bg-amber-100 text-amber-600 flex items-center justify-center comparison-icon transition-transform duration-300">
                            <i class="fas fa-lock-open text-2xl"></i>
                        </div>
                    </div>
                    <h3 class="text-xl font-semibold text-center mb-3">synchronized vs ReentrantLock</h3>
                    <ul class="text-slate-600 space-y-2">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-amber-500 mt-1 mr-2 text-sm"></i>
                            <span><code class="bg-amber-50 text-amber-600 px-1 rounded">ReentrantLock</code> 提供了更多的控制，如公平锁、可中断锁、条件变量等</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-amber-500 mt-1 mr-2 text-sm"></i>
                            <span><code class="bg-amber-50 text-amber-600 px-1 rounded">synchronized</code> 是隐式锁，使用更简单</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-amber-500 mt-1 mr-2 text-sm"></i>
                            <span><code class="bg-amber-50 text-amber-600 px-1 rounded">synchronized</code> 在 JDK 6+ 经过优化，性能差距减小</span>
                        </li>
                    </ul>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-6 comparison-card transition-all duration-300 group">
                    <div class="flex items-center justify-center mb-4">
                        <div class="w-16 h-16 rounded-full bg-amber-100 text-amber-600 flex items-center justify-center comparison-icon transition-transform duration-300">
                            <i class="fas fa-bolt text-2xl"></i>
                        </div>
                    </div>
                    <h3 class="text-xl font-semibold text-center mb-3">synchronized vs volatile</h3>
                    <ul class="text-slate-600 space-y-2">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-amber-500 mt-1 mr-2 text-sm"></i>
                            <span><code class="bg-amber-50 text-amber-600 px-1 rounded">volatile</code> 只保证变量的可见性，不保证操作的原子性</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-amber-500 mt-1 mr-2 text-sm"></i>
                            <span><code class="bg-amber-50 text-amber-600 px-1 rounded">synchronized</code> 可以确保对代码块的所有操作都是原子的</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-amber-500 mt-1 mr-2 text-sm"></i>
                            <span><code class="bg-amber-50 text-amber-600 px-1 rounded">volatile</code> 不会引起线程上下文切换，性能更高</span>
                        </li>
                    </ul>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-6 comparison-card transition-all duration-300 group">
                    <div class="flex items-center justify-center mb-4">
                        <div class="w-16 h-16 rounded-full bg-amber-100 text-amber-600 flex items-center justify-center comparison-icon transition-transform duration-300">
                            <i class="fas fa-exchange-alt text-2xl"></i>
                        </div>
                    </div>
                    <h3 class="text-xl font-semibold text-center mb-3">synchronized vs CAS</h3>
                    <ul class="text-slate-600 space-y-2">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-amber-500 mt-1 mr-2 text-sm"></i>
                            <span>CAS 是一种无锁机制，利用 CPU 指令集直接在内存中比较并替换数据</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-amber-500 mt-1 mr-2 text-sm"></i>
                            <span>CAS 在高并发下性能较好，但编程复杂度更高</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-amber-500 mt-1 mr-2 text-sm"></i>
                            <span>CAS 容易出现 ABA 问题</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>
    </div>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis',
                nodeSpacing: 50,
                rankSpacing: 50
            },
            sequenceDiagram: {
                useMaxWidth: true,
                diagramMarginX: 50,
                diagramMarginY: 10,
                actorMargin: 50,
                boxMargin: 10
            }
        });
    </script>
</body>
</html>
```